<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">订单管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            @clear="clearInput"
            clearable
            v-model="queryInfo.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="orderSearch"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表单区域 -->
      <el-row>
        <el-table :data="orderTableData" border style="width: 100%">
          <el-table-column type="index" label="#"></el-table-column>
          <el-table-column
            label="订单编号"
            prop="order_number"
            width="480"
          ></el-table-column>
          <el-table-column
            label="订单价格"
            prop="order_price"
            width="180"
          ></el-table-column>
          <el-table-column label="是否付款" prop="pay_status" width="180">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.pay_status === 1"
                >已付款</el-tag
              >
              <el-tag type="danger" v-else>未付款</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="是否发货"
            prop="is_send"
            width="180"
          ></el-table-column>
          <el-table-column label="下单时间" prop="create_time" width="180">
            <template slot-scope="scope">
              {{ scope.row.create_time | dateFormat }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <el-tooltip
              effect="dark"
              content=" 修改"
              :enterable="false"
              placement="top-start"
            >
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="showBox"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              effect="dark"
              content="物流进度"
              :enterable="false"
              placement="top-start"
            >
              <el-button
                type="success"
                size="mini"
                icon="el-icon-delete-location"
                @click="showProgressBox"
              ></el-button>
            </el-tooltip>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <el-pagination
          @size-change="orderHandleSizeChange"
          @current-change="orderHandleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 3, 5, 10]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 修改按钮的对话框 -->

    <el-dialog
      title="修改地址"
      @close="addressFormClose"
      :visible.sync="editDialogVisible"
      width="50%"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区县" prop="address1">
          <!-- 级联选择器 -->
          <!-- 选择商品分类的级联框 -->
          <!-- 级联选择器 -->
          <!-- options用来指定数据源 -->
          <!-- props 用来指定配置对象 -->
          <el-cascader
            ref="orderHandleRef"
            v-model="addressForm.address1"
            :options="cityDate"
            :props="orderProps"
            @change="orderHandleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <el-timeline>
        <!-- 时间线 -->
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="progressVisible = false">取 消</el-button>
        <el-button type="primary" @click="progressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入省市区县数据
import cityDate from './citydata.js'
export default {
  name: 'WorkspaceJsonOrder',
  data () {
    return {
      // 搜索条件对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 1
      },
      // 表单数据
      orderTableData: [],
      // 数据总条数
      total: 0,
      // 编辑对话框
      editDialogVisible: false,
      // 编辑对话框里的地址绑定数据
      addressForm: {
        // 级联选择其选择的值保存的位置
        address1: [],
        // 详细地址
        address2: ''
      },
      // 编辑对话框的验证规则
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      // 省市区县数据
      cityDate,
      // 级联选择器的配置项
      // children子节点值选项跟请求返回的名字要一致
      // expandTrigger子节点显示方式
      orderProps: {
        value: 'value',
        label: 'label',
        children: 'children',
        expandTrigger: 'hover'
      },
      // 物流进度对话框
      progressVisible: false,
      progressInfo: []
    }
  },
  created () {
    this.getOrderList()
  },
  mounted () {},
  methods: {
    getOrderList: async function () {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.orderTableData = res.data.goods
      this.total = res.data.total
    },
    // 每页条数变化时
    orderHandleSizeChange: function (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 页码变化时
    orderHandleCurrentChange: function (newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 监听搜索按钮
    orderSearch: function () {
      this.getOrderList()
      console.log(this.queryInfo)
    },
    // 清空数据时触发
    clearInput: function () {
      this.getOrderList()
    },
    // 展示修改按钮的对话框
    showBox: function () {
      this.editDialogVisible = true
    },
    // 当级联选择器的值发生变化时
    orderHandleChange: function () {},
    // 编辑对话框关闭时
    addressFormClose: function () {
      this.$refs.addressFormRef.resetFields()
      console.log(this.$refs.addressFormRef)
    },
    // 点击查看物流进度
    showProgressBox: async function () {
      // 获取物流信息(自己去开启这个根目录下js下的app.js)
      const { data: res } = await this.$http.get('http://localhost:3000/kuaidi/1106975712662')
      if (res.meta.status !== 200) return this.$message.error(res.meta.message)
      this.$message.success(res.meta.message)
      this.progressInfo = res.data
      this.progressVisible = true
    }
  },
  computed: {},
  watch: {
    // 实现选中后关闭级联的下拉框
    selectedKeys: function () {
      if (this.$refs.refHandle) {
        this.$refs.refHandle.dropDownVisible = false // 监听值发生变化就关闭它
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}
</style>
